package com.shz.appletsapi.config.security.social;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.security.crypto.encrypt.Encryptors;
import org.springframework.social.UserIdSource;
import org.springframework.social.config.annotation.ConnectionFactoryConfigurer;
import org.springframework.social.config.annotation.EnableSocial;
import org.springframework.social.config.annotation.SocialConfigurerAdapter;
import org.springframework.social.connect.ConnectionFactoryLocator;
import org.springframework.social.connect.ConnectionRepository;
import org.springframework.social.connect.ConnectionSignUp;
import org.springframework.social.connect.UsersConnectionRepository;
import org.springframework.social.connect.jdbc.JdbcUsersConnectionRepository;
import org.springframework.social.connect.web.ConnectController;
import org.springframework.social.connect.web.GenericConnectionStatusView;
import org.springframework.social.connect.web.ProviderSignInUtils;
import org.springframework.social.security.AuthenticationNameUserIdSource;

import com.shz.appletsapi.config.security.social.github.connect.GitHubConnectionFactory;
import com.shz.appletsapi.config.security.social.support.GitHubConnectView;
import com.shz.appletsapi.config.security.social.support.JsonConnectView;

@Configuration
@EnableSocial
public class SocialConfig extends SocialConfigurerAdapter {

	@Autowired
	private DataSource dataSource;

	private static final String apiKey = "a8b33e04b7e92c3b07bc";
	private static final String appSecret = "a93898abd49318b4ae31270db589ed2b59adc700";
	
	@Autowired(required = false)
	private ConnectionSignUp zyConnectionSignUp;
	@Override
	public UsersConnectionRepository getUsersConnectionRepository(ConnectionFactoryLocator connectionFactoryLocator) {
		JdbcUsersConnectionRepository jdbcUsersConnectionRepository = new JdbcUsersConnectionRepository(dataSource,
				connectionFactoryLocator, Encryptors.noOpText());
		if(zyConnectionSignUp != null) {
			jdbcUsersConnectionRepository.setConnectionSignUp(zyConnectionSignUp);
		}
		return jdbcUsersConnectionRepository;
	}

	@Override
	public UserIdSource getUserIdSource() {
		// TODO Auto-generated method stub
		return new AuthenticationNameUserIdSource();
	}


	@Override
	public void addConnectionFactories(ConnectionFactoryConfigurer connectionFactoryConfigurer,
			Environment environment) {
		// TODO Auto-generated method stub
		connectionFactoryConfigurer.addConnectionFactory(new GitHubConnectionFactory(apiKey, appSecret));
	}

	@Configuration
	static class SocialSupportConfig {

		@Bean
		public ProviderSignInUtils getProviderSignInUtils(ConnectionFactoryLocator connectionFactoryLocator,
				UsersConnectionRepository usersConnectionRepository) {
			return new ProviderSignInUtils(connectionFactoryLocator, usersConnectionRepository);
		}

		@Bean
		public ConnectController connectController(ConnectionFactoryLocator connectionFactoryLocator,
				ConnectionRepository connectionRepository) {
			ConnectController controller = new ConnectController(connectionFactoryLocator,connectionRepository);
			return controller;
		}

		@Bean(name={"connect/status"})
		public JsonConnectView getJsonConnectView() {
			return new JsonConnectView();
		}
		
		@Bean(name={"connect/githubConnect","connect/githubConnected"})
		public GitHubConnectView getGenericConnectionStatusView() {
			return new GitHubConnectView("github","GitHub");
		}
		
	/*	@Bean(name="connect/githubConnected")
		public BDSuccessConnectView getBDSuccessConnectView() {
			return new BDSuccessConnectView();
		}*/


	}

}
